package com.cskaoyan.service.mall;


import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.mapper.RegionMapper;
import com.cskaoyan.model.bean.*;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.bo.mall.BrandCreatBo;
import com.cskaoyan.model.vo.mall.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.lang.System;
import java.util.ArrayList;
import java.util.List;

@Service
public class MallServiceImpl implements MallService {

    @Autowired
    RegionMapper regionMapper;

    // /admin/region/list
    @Override
    public List<RegionBeanVo> getRegionList() {

        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        criteria.andIdIsNotNull().andPidEqualTo(0);
        List<Region> regions = regionMapper.selectByExample(regionExample);

        //拷贝进自己vo
        List<RegionBeanVo> regionBeanVoList = new ArrayList<>();
        for (int i = 0; i < regions.size(); i++) {
            //获得省结点
            Region region = regions.get(i);
            //创建新的省VO结点
            RegionBeanVo regionBeanVo = new RegionBeanVo();
            regionBeanVo.setId(region.getId());
            regionBeanVo.setCode(region.getCode());
            regionBeanVo.setPid(region.getPid());
            regionBeanVo.setName(region.getName());
            regionBeanVo.setType(region.getType());
            //省Vo结点还差city没搞定
            //开始遍历 city结点
            RegionExample cityExample = new RegionExample();
            RegionExample.Criteria cityExampleCriteria = cityExample.createCriteria();
            cityExampleCriteria.andPidEqualTo(regionBeanVo.getId()).andIdIsNotNull();
            List<Region> cityListFromSQL = regionMapper.selectByExample(cityExample);
            List<Children> cityList = new ArrayList<>();
            for (int j = 0; j < cityListFromSQL.size(); j++) {
                //封装城市
                Region city = cityListFromSQL.get(j);
                Children children = new Children();
                children.setCode(city.getCode());
                children.setId(city.getId());
                children.setName(city.getName());
                children.setType(city.getType());
                //查询城市中的街道
                RegionExample streetExample = new RegionExample();
                RegionExample.Criteria streetExampleCriteria = streetExample.createCriteria();
                streetExampleCriteria.andPidEqualTo(city.getId()).andIdIsNotNull();
                List<Region> streetList = regionMapper.selectByExample(streetExample);
                children.setChildren(streetList);
                cityList.add(children);
            }
            regionBeanVo.setChildren(cityList);

            regionBeanVoList.add(regionBeanVo);
        }


        System.out.println(regions);


        return regionBeanVoList;
    }


    @Autowired
    BrandMapper brandMapper;

    @Override
    // /admin/brand/list?page=1&limit=20&sort=add_time&order=desc
    public BrandVo getBrandList(PageBo pageBo, Integer id, String name) {

        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());

        BrandExample brandExample = new BrandExample();
        brandExample.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());//根据￥做sql语句拼接
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        List<Brand> brands = brandMapper.selectByExample(brandExample);

        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        long total = brandPageInfo.getTotal();

        BrandVo brandVo = new BrandVo();
        brandVo.setItems(brands);
        brandVo.setTotal(total);
        return brandVo;
    }


    @Override
    public Brand brandCreate(@RequestBody BrandCreatBo brandCreatBo) {

        Brand brand = new Brand();
        brand.setName(brandCreatBo.getName());
        brand.setDesc(brandCreatBo.getDesc());
        brand.setPicUrl(brandCreatBo.getPicUrl());
        brand.setFloorPrice(brandCreatBo.getFloorPrice());
//        Date date = new Date();
//        brand.setAddTime(date);
        //修改了sql语句，addTime和updatetime会自动注入
        //插入
        brandMapper.insertSelective(brand);

        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andIdEqualTo(brand.getId());
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        brand = brands.get(0);
        return brand;
    }

    @Override
    public Brand brandUpdate(Brand brand) {

        Brand brand1 = brandMapper.selectByPrimaryKey(brand.getId());
        if (brand1 == null) {
            return null;
        }
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andIdEqualTo(brand.getId());
        int succes = brandMapper.updateByPrimaryKey(brand);
        if (succes == 1) {
            return brand;
        } else {
            return null;
        }
    }

    @Override
    public int brandDelete(int id) {

        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andIdEqualTo(id);

        Brand brand = new Brand();
        brand.setDeleted(true);

        int i = brandMapper.updateByExampleSelective(brand, brandExample);

        if (i == 1) {
            return 1;
        } else {
            return -1;
        }

    }


    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<CategoryHasChildrenVo> getCategoryList() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andIdIsNotNull().andLevelEqualTo("L1");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);

        List<CategoryHasChildrenVo> hasChildrenVoList = new ArrayList<>();
        for (int i = 0; i < categories.size(); i++) {
            Category category = categories.get(i);
            CategoryHasChildrenVo categoryHasChildrenVo = new CategoryHasChildrenVo();
            categoryHasChildrenVo.setDesc(category.getDesc());
            categoryHasChildrenVo.setIconUrl(category.getIconUrl());
            categoryHasChildrenVo.setId(category.getId());
            categoryHasChildrenVo.setKeywords(category.getKeywords());
            categoryHasChildrenVo.setName(category.getName());
            categoryHasChildrenVo.setPicUrl(category.getPicUrl());
            categoryHasChildrenVo.setLevel(category.getLevel());

            CategoryExample categoryChildrenExample = new CategoryExample();
            CategoryExample.Criteria childrenExampleCriteria = categoryChildrenExample.createCriteria();
            childrenExampleCriteria
                    .andIdIsNotNull()
                    .andPidEqualTo(categoryHasChildrenVo.getId());
//                    .andLevelEqualTo("L2");
            List<Category> childrenList = categoryMapper.selectByExample(categoryChildrenExample);
            categoryHasChildrenVo.setChildren(childrenList);

            hasChildrenVoList.add(categoryHasChildrenVo);
        }
        return hasChildrenVoList;
    }

    @Override
    public List<CategoryL1Vo> getL1List() {


        return categoryMapper.selectByL1();
    }

    @Override
    public int categoryUpdate(Category category) {

        int i = categoryMapper.updateByPrimaryKeySelective(category);
        if (i == 1) {
            return 1;
        } else {
            return -1;
        }
    }

    @Override
    public int categoryCreat(Category category) {
        int i = categoryMapper.insertSelective(category);
        if (i == 1) {
            return 1;
        } else {
            return -1;
        }
    }

    @Override
    public int categoryDelete(Integer id) {
        Category category = new Category();
        category.setId(id);
        category.setDeleted(true);
        int i = categoryMapper.insertSelective(category);
        if (i == 1) {
            return 1;
        } else {
            return -1;
        }
    }
}
